package com.project.nimsen.web.pub.tag;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Component;

import com.project.nimsen.constant.NimsenConstant;
import com.project.nimsen.dto.ColfileDto;
import com.project.nimsen.dto.ColumnDto;
import com.project.nimsen.entity.Coltype;
import com.project.nimsen.service.IColfileService;
import com.project.nimsen.service.IColtypeService;
import com.project.nimsen.util.NimsenUtil;
import com.project.nimsen.util.WebUtil;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
/**
 * 用于得到一系列关于栏目的信息
 * */
@Component("nimsen_columns")
public class ColumnsTag extends BaseTag {
	@Resource
	IColfileService colfileService;
	@Resource
	IColtypeService coltypeService;
	/**
	 * @param number PARENT与父级栏目同级的所有栏目如果当前栏目是顶级栏目则返回所有
	 * 				   顶级栏目，OWNER与当前栏目同级的所有栏目，
	 * 				 CHILD表示当前栏目下的直属子栏目，TOPARENT则为整个项目的顶级
	 * 				   栏目列表，其他为指定栏目下的直属子栏目，此项为必选项目
	 * @param num 显示查询栏目信息的前几个栏目
	 * @param orderType 可选，ASC为长升序，DESC为降序
	 * 
	 * @return 返回的集合值为 columns
	 * */
	public void exec(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		
		String number = getParam("number");
		//必选项验证
		if(StringUtils.isBlank(number))
			return;
		String colid = getAttr("coltypeid");
		Coltype curcol = null;
		try {
			ColumnDto colDto = new ColumnDto();
			//站点id
			colDto.setSiteid(getSessionAttr("pub_siteid"));
			//如果没有指定的是当前栏目的关系进行验证
			if("PARENT".equals(number)||"OWNER".equals(number)||"CHILD".equals(number)){
				if(colid==null)
					return;
				curcol = coltypeService.findEntityById(colid);
				//如果没有当前栏目的处理情况
				if(curcol==null)
					return;
				colDto.setCurrnumber(curcol.getNumber());
				colDto.setPnumber(curcol.getPnumber());
			}
			//查询条件设置
			colDto.setNumber(number);
			String num = getParam("num");
			if(StringUtils.isNotBlank(num))
				colDto.setNum(Integer.parseInt(num));
			String orderType = getParam("orderType");
			if(StringUtils.isNotBlank(orderType)&&"DESC".equals(orderType))
				colDto.setOrderType(orderType);
			else
				colDto.setOrderType("ASC");
			//查询栏目信息
			List<Map<String, Object>> list = coltypeService.findTagByDto(colDto);
			if(CollectionUtils.isEmpty(list))
				return;
			for(Map<String, Object> map : list){
				Object urlObj = map.get("vurl");
				map.put("vurl", WebUtil.webUrl(request)+"/pub/col"+urlObj);
				//查询栏目缩略图
				ColfileDto fileDto = new ColfileDto();
				fileDto.setRelatiovalue(NimsenConstant.FILE_RELATION_MICROIMG);
				String columnid = NimsenUtil.obj2String(map.get("id"));
				if(StringUtils.isNotBlank(columnid))
					fileDto.setColtypeid(columnid);
				List<Map<String, Object>> micromigs = colfileService.findTagFileByDto(fileDto);
				if(!CollectionUtils.isEmpty(micromigs)){
					Map<String, Object> microimg = micromigs.get(0);
					map.put("microimgurl", microimg.get("filename"));
				}
			}
			//存取到页面
			outVariable("columns", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}